//
//  XMClassListViewController.m
//  Second
//
//  Created by xalo on 16/7/16.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "XMClassListViewController.h"

@interface XMClassListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)UICollectionView *collertionView;
// cell上的图片
@property (nonatomic,strong)NSMutableArray *datasource;
//最新列表的数据的起始位置
@property (nonatomic,assign)NSInteger newStart;
@property (nonatomic,assign)NSInteger pageid;
@property (nonatomic,assign)BOOL isLoad;
@property (nonatomic,strong)UIButton *leftBtn;
@property (nonatomic,strong)UIButton *rightBtn;
@property (nonatomic,strong)UIButton *Btn;

//@property (nonatomic, strong) LrdSuperMenu *menu;
//@property (nonatomic, strong) NSArray *sort;
//@property (nonatomic, strong) NSArray *choose;
//@property (nonatomic, strong) NSArray *classify;
//@property (nonatomic, strong) NSArray *jiachang;
//@property (nonatomic, strong) NSArray *difang;
//@property (nonatomic, strong) NSArray *tese;
//@property (nonatomic, strong) NSArray *rihan;
//@property (nonatomic, strong) NSArray *xishi;
//@property (nonatomic, strong) NSArray *shaokao;

@end

@implementation XMClassListViewController

- (UICollectionView *)collertionView {
    
    if (!_collertionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        _collertionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64) collectionViewLayout:layout];
        _collertionView.dataSource = self;
        _collertionView.delegate = self;
        layout.itemSize = CGSizeMake(kWidth/2-15, kWidth/2+50);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
        
        
    }
    return _collertionView;
}

- (NSMutableArray *)datasource {
    
    if (!_datasource) {
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collertionView registerNib:[UINib nibWithNibName:@"ClassListCell" bundle:nil] forCellWithReuseIdentifier:@"ClassListCell"];
    
    self.collertionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collertionView];
    
    //数据请求
    self.pageid = 1;
    [self loadClassListWithPageid:self.pageid];
    //添加下拉刷新
    self.collertionView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        
        self.isLoad = YES;
        [self loadClassListWithPageid:self.pageid];
        
    }];
    
    //添加上拉加载
    self.collertionView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.pageid++;
        [self loadClassListWithPageid:self.pageid];
        
    }];
    
    
//    self.classify = @[@"新店特惠", @"连锁餐厅", @"家常快餐", @"地方菜", @"特色小吃", @"日韩料理", @"西式快餐", @"烧烤海鲜"];
//    
//    self.sort = @[@"智能排序", @"销量最高", @"距离最近", @"评分最高", @"起送价最低", @"送餐速度最快"];
//    
//    self.choose = @[@"筛选", @"立减优惠", @"预定优惠", @"特价优惠", @"折扣商品", @"进店领券", @"下单返券"];
//    
//    self.jiachang = @[@"家常炒菜", @"黄焖J8饭", @"麻辣烫", @"盖饭"];
//    self.difang = @[@"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜"];
//    self.tese = @[@"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜"];
//    self.rihan = @[@"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜"];
//    self.xishi = @[@"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜"];
//    self.shaokao = @[@"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜", @"湘菜"];
//    
//    
//    _menu = [[LrdSuperMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:44];
//    _menu.delegate = self;
//    _menu.dataSource = self;
//    [self.view addSubview:_menu];
//    
//    //    [_menu selectDeafultIndexPath];

    
}

//加载数据
- (void)loadClassListWithPageid:(NSInteger)pageid{
    
    [RequestManager fecthDataWithUrl:@"http://youzixy.com/app/sale/goods" parameter:@{@"type":self.typeID,@"page":@(pageid)} mehtod:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
   //     NSLog(@"%@",bigdic);
        
        if (self.isLoad && bigdic.count!=0) {
            [self.datasource removeAllObjects];
            self.isLoad = NO;
        }
        
        for (NSDictionary *dic in bigdic[@"res"]) {
            
            ClassListModel *model = [[ClassListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.datasource addObject:model];
        }
        
    } updateUI:^{

        //结束上拉加载
        [self.collertionView.mj_footer endRefreshing];
        //结束下拉刷新
        [self.collertionView.mj_header endRefreshing];
        //刷新tableView
        
        
        [self.collertionView reloadData];
    }];
    
}

#pragma mark -----------collectionViewDegelate-----------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.datasource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ClassListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassListCell" forIndexPath:indexPath];
    [cell setCellWithModelll:self.datasource[indexPath.row]];
    
    cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 10;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XMSecondController *second = [[XMSecondController alloc]init];
    second.saleid = [self.datasource[indexPath.row] saleid];
    [self.navigationController pushViewController:second animated:YES];
    
    
}




//
//- (NSInteger)numberOfColumnsInMenu:(LrdSuperMenu *)menu {
//    return 3;
//}
//
//- (NSInteger)menu:(LrdSuperMenu *)menu numberOfRowsInColumn:(NSInteger)column {
//    
//    if (column == 0) {
//        
//        return self.classify.count;
//    }else if(column == 1) {
//        
//        return self.sort.count;
//    }else {
//        
//        return self.choose.count;
//    }
//}
//
//- (NSString *)menu:(LrdSuperMenu *)menu titleForRowAtIndexPath:(LrdIndexPath *)indexPath {
//    
//    if (indexPath.column == 0) {
//        
//        return self.classify[indexPath.row];
//    }else if(indexPath.column == 1) {
//        
//        return self.sort[indexPath.row];
//    }else {
//        
//        return self.choose[indexPath.row];
//    }
//}
//
////- (NSString *)menu:(LrdSuperMenu *)menu imageNameForRowAtIndexPath:(LrdIndexPath *)indexPath {
////    if (indexPath.column == 0 || indexPath.column == 1) {
////        return @"baidu";
////    }
////    return nil;
////}
////
////- (NSString *)menu:(LrdSuperMenu *)menu imageForItemsInRowAtIndexPath:(LrdIndexPath *)indexPath {
////    if (indexPath.column == 0 && indexPath.item >= 0) {
////        return @"baidu";
////    }
////    return nil;
////}
////
////- (NSString *)menu:(LrdSuperMenu *)menu detailTextForRowAtIndexPath:(LrdIndexPath *)indexPath {
////    if (indexPath.column < 2) {
////        return [@(arc4random()%1000) stringValue];
////    }
////    return nil;
////}
//
//- (NSString *)menu:(LrdSuperMenu *)menu detailTextForItemsInRowAtIndexPath:(LrdIndexPath *)indexPath {
//    return [@(arc4random()%1000) stringValue];
//}
//
//- (NSInteger)menu:(LrdSuperMenu *)menu numberOfItemsInRow:(NSInteger)row inColumn:(NSInteger)column {
//    if (column == 0) {
//        if (row == 0) {
//            return self.jiachang.count;
//        }else if (row == 4) {
//            return self.difang.count;
//        }else if (row == 5) {
//            return self.tese.count;
//        }else if (row == 6) {
//            return self.rihan.count;
//        }else if (row == 7) {
//            return self.xishi.count;
//        }else if (row == 8) {
//            return self.shaokao.count;
//        }
//    }
//    return 0;
//}
//
//- (NSString *)menu:(LrdSuperMenu *)menu titleForItemsInRowAtIndexPath:(LrdIndexPath *)indexPath {
//    NSInteger row = indexPath.row;
//    if (indexPath.column == 0) {
//        if (row == 3) {
//            
//            return self.jiachang[indexPath.item];
//        }else if (row == 4) {
//            return self.tese[indexPath.item];
//        }else if (row == 5) {
//            return self.rihan[indexPath.item];
//        }else if (row == 6) {
//            return self.xishi[indexPath.item];
//        }else if (row == 7) {
//            return self.shaokao[indexPath.item];
//        }
//    }
//    return nil;
//}
//
//- (void)menu:(LrdSuperMenu *)menu didSelectRowAtIndexPath:(LrdIndexPath *)indexPath {
//    if (indexPath.item >= 0) {
//        NSLog(@"点击了 %ld - %ld - %ld 项目",indexPath.column,indexPath.row,indexPath.item);
//    }else {
//        NSLog(@"点击了 %ld - %ld 项目",indexPath.column,indexPath.row);
//    }
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
